package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.ProcessCategoryPO;
import com.hyzx.qbasic.admin.domain.dao.ProcessCategoryMapper;
import com.hyzx.qbasic.admin.domain.dao.ProcessConfigMapper;
import com.hyzx.qbasic.admin.domain.dao.ProcessDefinitionMapper;
import com.hyzx.qbasic.admin.domain.repository.IProcessCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 黄江华 on 2018/11/21
 */

@Repository
public class ProcessCategoryRepositoryImpl implements IProcessCategoryRepository {

    @Autowired
    private ProcessCategoryMapper categoryMapper;

    @Autowired
    private ProcessDefinitionMapper definitionMapper;

    @Autowired
    private ProcessConfigMapper configMapper;

    @Override
    public List<ProcessCategoryPO> getAllProcessCategoryList() {
        return categoryMapper.getAllProcessCategoryList();
    }

    @Override
    public PageInfo<ProcessCategoryPO> getPagedProcessCategoryList(int pageNum, int pageSize, String processCategoryName) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> categoryMapper.getPagedProcessCategoryList(processCategoryName));

        return pageInfo;
    }

    @Override
    public boolean addProcessCategory(ProcessCategoryPO category) {
        return categoryMapper.addProcessCategory(category) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProcessCategory(List<Integer> processCategoryIdList) {
        boolean success = categoryMapper.deleteProcessCategory(processCategoryIdList) > 0;
        if (success) {
            definitionMapper.deleteProcessCategory(processCategoryIdList);
            configMapper.deleteProcessCategory(processCategoryIdList);
        }

        return success;
    }

    @Override
    public ProcessCategoryPO getProcessCategoryById(int processCategoryId) {
        return categoryMapper.getProcessCategoryById(processCategoryId);
    }

    @Override
    public ProcessCategoryPO getProcessCategoryByKey(String processCategoryKey) {
        return categoryMapper.getProcessCategoryByKey(processCategoryKey);
    }

    @Override
    public boolean editProcessCategory(ProcessCategoryPO category) {
        return categoryMapper.editProcessCategory(category) > 0;
    }
}
